Introduction
function is what we do.fisrt container put codes in. how long ?20,30lines? there is a simple rule to follow. how to find classes by getting your function at right size. do one thing , do it well, do it only. but what does onething mean?
what makes sun shine
the first rule of funtions
They should be smaller.The second rule of functions that should small than that.
How big should a funtion be?
Back in the 1990s, we have a simple rule, a function should be a screen full.(about 20 lines.)
original the screen full is out of date,doesn't apply anymore.
"Four lines is ok, maybe five, six? ok,ten is way too big."
sound insame.
In a four line function ,how many indent how many if else statement,try catch blocks your can see.there is not enough room for indenting.
small functions have nice long descriptive names,
what's more, you will extrat the pridicts of your if statements and while loop into even more nice small named boolean functions.
example:
-
before:
123456789public String invoke() throws Exception{if(pageData.hasAttribute("Test")){content+= includeSetups();content+=page.getContent()+"\n";content+=includeTeardowns();pageData.setContent(content.toString());}return page.getHtml();} -
after
123456789101112public String invoke() throws Exception{if(isTestPage()){content+= includeSetups();content+=page.getContent()+"\n";content+=includeTeardowns();pageData.setContent(content.toString());}return page.getHtml();}private boolean isTestPage() throw Exception{return pageData.hasAttribute("Test");}
Are you out of mind
you are thinking,you will lost in a sea of funtions
where do classes go to hide
a long function is where the classes go to hide long funtion could always refactor to a class
one thing
The old rule that "the function should do one thing, do it well and do it only." But what does onething mean? it could be any thing. at same level
extract untill your drop
extract till you can not extract
conculsion
- the fisrt rule of funtion is small;
- making functions smaller will save your and everybody time.
- the class hide in long functions.
- funtions do one thing. extract till you drop.